/*
 * srv.c
 *
 *  Created on: 2013. 3. 19.
 *      Author: root
 */

/*
 *
#include	"unp.h"

#define NAMESIZE 30
#define RESPONSESIZE 5
#define FILESIZE 1000000

int
main(int argc, char **argv)
{
	int					listenfd, connfd, filefd;
	int 				fileCnt, cnt = 0;
	char				buf[MAXLINE];
	char 				fileNameBuf[NAMESIZE];
	char 				responseBuf[RESPONSESIZE];
	pid_t				childpid;
	socklen_t			clilen;
	ssize_t 			n;
	struct sockaddr_in	cliaddr, servaddr;

	if(argc != 2) {
		printf("./FileServ <FIleName.txt>\n");
		exit(1);
	}

	strcpy(fileNameBuf, argv[1]);

	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(SERV_PORT);

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);

		puts("Client connection");
		printf("connection from %s, port : %d\n",inet_ntop(AF_INET, &cliaddr.sin_addr, buf, sizeof(buf)), ntohs(cliaddr.sin_port));

		if ( (childpid = Fork()) == 0) {	 child process
			Close(listenfd);	 close listening socket

			write(connfd, fileNameBuf, NAMESIZE);
			read(connfd, responseBuf, RESPONSESIZE);

			if(strcmp(responseBuf, "Y") == 0 || strcmp(responseBuf, "y") == 0) {
				if((filefd = open(fileNameBuf, "r")) == -1) {
						    printf("fopen error\n");
						    exit(0);
					}

				while((fileCnt = read(filefd, buf, MAXLINE)) != 0) {
					write(connfd, buf, fileCnt);
					printf("File Sending \r%d\n", cnt++);
					sleep(1);
				}

				printf("Clinet IP : %s, %s File Transmission Complete \n", inet_ntop(AF_INET, &cliaddr.sin_addr, buf, sizeof(buf)) , fileNameBuf);

			}

		}
		Close(filefd);
		Close(connfd);			 parent closes connected socket
	}
}*/

/*
#include	"unp.h"

#define NAMESIZE 30
#define RESPONSESIZE 5
#define FILESIZE 1000000

int
main(int argc, char **argv)
{
	int					listenfd, connfd, filefd;
	int 				fileCnt, cnt = 0;
	char				buf[MAXLINE];
	char 				fileNameBuf[NAMESIZE];
	char 				responseBuf[RESPONSESIZE];
	pid_t				childpid;
	socklen_t			clilen;
	ssize_t 			n;
	struct sockaddr_in	cliaddr, servaddr;

	if(argc != 1) {
		printf("./FileServ <FIleName.txt>\n");
		exit(1);
	}

	//strcpy(fileNameBuf, argv[1]);

	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(SERV_PORT);

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);

		puts("Client connection");
		printf("connection from %s, port : %d\n",inet_ntop(AF_INET, &cliaddr.sin_addr, buf, sizeof(buf)), ntohs(cliaddr.sin_port));


		filefd = open("test1.txt", O_RDONLY);

		if(filefd == -1) {
			printf("fopen error\n");
			exit(0);
		}


			while((fileCnt = read(filefd, buf, MAXLINE)) != 0) {
				write(connfd, buf, fileCnt);
				printf("File Sending \r%d\n", cnt++);
				//sleep(1);
			}

			printf("Clinet IP : %s, %s File Transmission Complete \n", inet_ntop(AF_INET, &cliaddr.sin_addr, buf, sizeof(buf)) , fileNameBuf);
	}
			Close(filefd);


close(listenfd);
	Close(connfd);
}
*/


